package com.hushui.mall.order.listener;

import com.hushui.common.to.mq.SeckillOrderTo;
import com.hushui.mall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


/**
 * @Author: hwh
 * @Date: 2023/03/20/14:58
 */
@Slf4j
@Component
public class SecKillListener {
    @Autowired
    private OrderService orderService;

    @RabbitListener(queues = "order.seckill.order.queue")
    public void listener(SeckillOrderTo orderTo, Message message,Channel channel) throws IOException {
        log.info("开始创建秒杀订单.....");
        System.out.println(orderTo);
        orderService.creatSeckillOrder(orderTo);

        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }

    }
}
